package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.WebListAdapter;
import com.sx.gongzhong.model.WebListItemBean;
import com.sx.gongzhong.pest.WebListPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.WebListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**平安宣传
 * Created by admin on 2018/1/3.
 */

public class PromotePeaceActivity  extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, WebListView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private int page = 1, pagesize = 10;
    private WebListAdapter adapter;
    private List<WebListItemBean> mlist;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_list);
        ButterKnife.bind(this);
        setTitleName("平安宣传");
        initView();
    }

    private void initView() {
        mlist = new ArrayList<>();
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(PromotePeaceActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new WebListAdapter(this, R.layout.item_convenniceannounce, mlist);
        adapter.setEmptyView(View.inflate(this, R.layout.load_error_view, null));
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(PromotePeaceActivity.this,WebActivity.class)
                        .putExtra("from",3)
                        .putExtra("detailUrl",mlist.get(position).detailUrl+"?id="+mlist.get(position).id));
            }
        });
        onRefresh();
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        page = 1;
        getData();
    }

    private void getData() {
        new WebListPest(this).getPromotePeaceData(this,page,pagesize);
    }

    @Override
    public void getSuccess(List<WebListItemBean> list, int total) {
        if (page == 1) {
            mlist.clear();
            adapter.setNewData(mlist);
        }
        if (list != null && list.size() > 0) {
            mlist.addAll(list);
        }
        if (page == total){
            adapter.loadMoreEnd();
        }else {
            adapter.loadMoreComplete();
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(PromotePeaceActivity.this,message);
    }
}
